package bacnetnetwork1;

import com.serotonin.bacnet4j.LocalDevice;
import com.serotonin.bacnet4j.exception.BACnetServiceException;
import com.serotonin.bacnet4j.obj.BACnetObject;
import com.serotonin.bacnet4j.type.enumerated.EngineeringUnits;
import com.serotonin.bacnet4j.type.enumerated.ObjectType;
import com.serotonin.bacnet4j.type.enumerated.PropertyIdentifier;
import com.serotonin.bacnet4j.type.primitive.*;
import com.serotonin.bacnet4j.type.primitive.Boolean;
import com.serotonin.bacnet4j.type.Encodable;



public class Sensor extends LocalDevice {
    
    public Sensor(int ID, String networkAddress, String deviceAddress, int port, String name, EngineeringUnits units) throws Exception {
        // Create Sensor as LocalDevice
        super(ID, networkAddress,deviceAddress);        
        this.getConfiguration().setProperty(PropertyIdentifier.objectName, new CharacterString(name));
        this.setPort(port);

        BACnetObject ai1 = new BACnetObject(this,
                this.getNextInstanceObjectIdentifier(ObjectType.analogInput));
        ai1.setProperty(PropertyIdentifier.objectName, new CharacterString("Sensor output"));
        ai1.setProperty(PropertyIdentifier.units, units);
        ai1.setProperty(PropertyIdentifier.outOfService, new Boolean(false));
        this.addObject(ai1);

        // Start device
        this.initialize();       
        
    }
    
    public void setSensorValue(double givenValue) throws BACnetServiceException {
        BACnetObject ai=this.getObject("Sensor output"); 
        ai.setProperty(PropertyIdentifier.presentValue, new Real((float) givenValue));
    }
    
    public void setOutOfService(boolean givenValue) throws BACnetServiceException {
        BACnetObject ai=this.getObject("Sensor output"); 
        ai.setProperty(PropertyIdentifier.outOfService, new Boolean(givenValue));
    }
    
    public Encodable getOutOfService() throws BACnetServiceException {        
        BACnetObject av=this.getObject("Sensor output"); 
        Encodable currentValue=av.getProperty(PropertyIdentifier.outOfService);
        return currentValue;        
    }
        
}
